from django.http import JsonResponse
from django.shortcuts import render, redirect
import json

from la import models
from la.form.form import SyrInfoModelForm, ZffsInfoModelForm, BrandInfoModelForm

from la.view.func import ishavesku

'''收银员信息'''
def syrinfo(req):
    """收银信息列表"""
    info = req.session.get( "info" )
    storeid = info['storecode']
    userinfos = models.sypeople.objects.filter(store_id=storeid).all()
    return render(req, "syrinfo.html", {"userinfos": userinfos})

def syrinfo_add(req):
    """用户信息添加操作"""
    if req.method == "GET":
        form = SyrInfoModelForm()
        return render(req, "syrinfo_add.html", {"form":form})
    form = SyrInfoModelForm(data=req.POST)
    if form.is_valid():
        form.instance.isty = 1
        form.save()
        return redirect("/la/syrinfo/")
    else:
        return render(req, "syrinfo_add.html", {"form":form})

def syrinfo_edit(req, nid):
    """用户信息修改操作"""
    if req.method == "GET":
        syrinfo = models.sypeople.objects.filter(id=nid).all().first()
        storeinfos = models.storeinfo.objects.filter(id__gt=1).all()
        form = SyrInfoModelForm(instance=syrinfo)
        return render(req, "syrinfo_edit.html", {"userinfo": syrinfo, "storeinfos": storeinfos, "form":form})
    syrinfo = models.sypeople.objects.filter(id=nid).all().first()
    form = SyrInfoModelForm(data=req.POST,instance=syrinfo)
    if form.is_valid():
        form.instance.isty = 1
        form.save()
        return redirect("/la/syrinfo/")
    else:
        return render(req, "syrinfo_edit.html", {"form": form})

'''支付方式'''

def zffsinfo(req):
    """收银信息列表"""
    info = req.session.get( "info" )
    zffsinfos = models.zffslist.objects.filter().all()
    return render(req, "zffsinfo.html", {"userinfos": zffsinfos})

def zffsinfo_add(req):
    """用户信息添加操作"""
    if req.method == "GET":
        form = ZffsInfoModelForm()
        return render(req, "zffsinfo_add.html", {"form":form})

    form = ZffsInfoModelForm(data=req.POST)
    if form.is_valid():
        form.instance.isty = 1
        form.save()
        return redirect("/la/zffsinfo/")
    else:
        return render(req, "zffsinfo_add.html", {"form":form})

def zffsinfo_edit(req, nid):
    """用户信息修改操作"""
    if req.method == "GET":
        zffsinfo = models.zffslist.objects.filter(id=nid).all().first()
        storeinfos = models.storeinfo.objects.filter(id__gt=1).all()
        form = ZffsInfoModelForm(instance=zffsinfo)
        return render(req, "zffsinfo_edit.html", {"userinfo": zffsinfo, "storeinfos": storeinfos, "form":form})
    zffsinfo = models.zffslist.objects.filter(id=nid).all().first()
    form = ZffsInfoModelForm(data=req.POST,instance=zffsinfo)
    if form.is_valid():
        form.instance.isty = 1
        form.save()
        return redirect("/la/zffsinfo/")
    else:
        return render(req, "zffsinfo_edit.html", {"form": form})



'''品牌信息操作'''
def brand(req):
    """收银信息列表"""
    info = req.session.get( "info" )
    brandinfo = models.brandlist.objects.filter().all()
    return render(req, "brand.html", {"userinfos": brandinfo})

def brand_add(req):
    """用户信息添加操作"""
    if req.method == "GET":
        form = BrandInfoModelForm()
        return render(req, "brand_add.html", {"form":form})
    form = BrandInfoModelForm(data=req.POST)
    if form.is_valid():
        form.instance.isty = 1
        form.save()
        return redirect("/la/brand/")
    else:
        return render(req, "brand_add.html", {"form":form})

def brand_edit(req, nid):
    """用户信息修改操作"""
    if req.method == "GET":
        brandinfo = models.brandlist.objects.filter(id=nid).all().first()
        storeinfos = models.storeinfo.objects.filter(id__gt=1).all()
        form = ZffsInfoModelForm(instance=brandinfo)
        return render(req, "brand_edit.html", {"userinfo": brandinfo, "storeinfos": storeinfos, "form":form})
    brandinfo = models.brandlist.objects.filter(id=nid).all().first()
    form = BrandInfoModelForm(data=req.POST,instance=brandinfo)
    if form.is_valid():
        form.instance.isty = 1
        form.save()
        return redirect("/la/brand/")
    else:
        return render(req, "brand_edit.html", {"form": form})


def package(req):
    """套餐信息列表"""
    info = req.session.get( "info" )
    packageinfo = models.package.objects.all()
    return render(req, "package_info.html", {"packageinfo": packageinfo})

def package_add(req):
    if req.method == "GET":
        return render(req,'package_info_add.html')
    if req.method == "POST":
        name = req.POST.get('name')
        price = req.POST.get('price')
        lis = req.POST.get( 'data' )
        re = json.loads( lis )
        s = ishavesku(re)
        if s:
            pi = models.package.objects.filter(name=name).first()
            if pi != None:
                re = {"re": "套餐名重复，操作失败"}
            else:
                packages = models.package.objects.create( name=name, price=price )
                for item in re:
                    if item["款号"] != '款号':
                        models.packagelist.objects.create(sku=item['款号'],num=int(item['数量']),package_id=packages.id)
                        re = {"re": "添加完成"}
        else:
            re = {"re": "操作失败"}
        return JsonResponse( re )

def package_seek(req,id):
    if req.method == 'GET':
        price = models.package.objects.filter(id=id).first().price
        name = models.package.objects.filter(id=id).first().name
        packagelist = models.packagelist.objects.filter(package=id).all()
        return render( req, "package_info_seek.html",{"name":name,"price":price,"packagelist":packagelist})